package com.book.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.book.dto.BookDto;
import com.book.entity.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BookDao extends BaseMapper<Book> {

    @Select("SELECT     \n" +
            "    b.id AS id,    \n" +
            "    b.book_name AS bookName,    \n" +
            "    b.author,    \n" +
            "    b.price,    \n" +
            "    b.publication_date,    \n" +
            "    b.content as content,    \n" +
            "    (SELECT img FROM book_image WHERE book_id = b.id and del_flag=0  ORDER BY id LIMIT 1) AS cover,\n" +
            "    (SELECT GROUP_CONCAT(img) FROM book_image WHERE book_id = b.id and del_flag=0) AS imgUrl, \n" +
            "    (SELECT GROUP_CONCAT(class_name) FROM book_class bc JOIN book_classes bcs ON bc.id = bcs.class_id WHERE bcs.book_id = b.id and bc.del_flag=0 and bcs.del_flag=0) AS className    \n" +
            "FROM     \n" +
            "    book b    \n" +
            "WHERE     \n" +
            "    (b.book_name LIKE CONCAT('%', #{bookName}, '%'))    \n" +
            "    AND (b.author LIKE CONCAT('%', #{author}, '%'))    \n" +
            "    AND (b.price >= COALESCE(NULLIF(#{startPrice}, ''), 0)) \n"+
            "    AND (b.price <= COALESCE(NULLIF(#{endPrice}, ''), 9999))    \n" +
            "    AND b.id IN (    \n" +
            "        SELECT book_id FROM book_classes WHERE (class_id = #{classId} OR #{classId} IS NULL) and del_flag=0    \n" +
            "    )  and b.del_flag=0  \n" +
            "ORDER BY     \n" +
            "    CASE WHEN #{pricesort} THEN b.price ELSE -b.price END")
    List<BookDto> selectBooksWithDetailsAndPagination(//这里是写的获取所有图书信息）
            @Param("bookName") String bookName,
            @Param("author") String author,
            @Param("startPrice") String startPrice,
            @Param("endPrice") String endPrice,
            @Param("pricesort") boolean pricesort,
            @Param("classId") Integer classId
    );

    @Select("SELECT     \n" +
            "    b.id AS id,    \n" +
            "    b.book_name AS bookName,    \n" +
            "    b.author,    \n" +
            "    b.price,    \n" +
            "    b.publication_date,    \n" +
            "    b.content as content,    \n" +
            "    b.book_no as bookNo,    \n" +
            "    b.publish as publish,    \n" +
            "    (SELECT GROUP_CONCAT(class_name) FROM book_class bc JOIN book_classes bcs ON bc.id = bcs.class_id WHERE bcs.book_id = b.id and bc.del_flag=0 and bcs.del_flag=0) AS className    \n" +
            "FROM     \n" +
            "    book b    \n" +
            "WHERE     \n" +
            "    (b.book_name LIKE CONCAT('%', #{bookName}, '%'))    \n" +
            "    and b.del_flag=0  \n" +
            "ORDER BY     \n" +
            "    b.id desc")
    List<BookDto> getBooks(//这里是写的获取所有图书信息）
          @Param("bookName") String bookName
    );

}